//
//  YMProductSpecificationListCell.m
//  YMMedical
//
//  Created by y s on 2023/7/26.
//

#import "YMProductSpecificationListCell.h"
#import "YMNetwork.h"

@interface YMProductSpecificationListCell ()

@property (nonatomic, strong) UIView *vBg;
@property (nonatomic, strong) UIImageView *imgvIcon;
@property (nonatomic, strong) UILabel *lblName;

@property (nonatomic, strong) UIButton *btnDownload;

@end

@implementation YMProductSpecificationListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark - UI methods
- (void)setUI{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
    
    [self.contentView addSubview:self.vBg];
    [self.vBg addSubview:self.lblName];
    [self.vBg addSubview:self.imgvIcon];
    [self.vBg addSubview:self.btnDownload];

    [self.vBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.top.mas_equalTo(0);
    }];
    [self.imgvIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo([SFitUtil fitSize:36 height:36]);
    }];
    [self.lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.btnDownload.mas_left);
        make.centerY.mas_equalTo(0);
        make.left.equalTo(self.imgvIcon.mas_right).offset([SFitUtil fit:15]);
    }];
    [self.btnDownload mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_equalTo(0);
        make.width.mas_equalTo([SFitUtil fit:54]);
    }];
}


#pragma mark - public methods
+ (CGFloat)getCellHeight{
    return [SFitUtil fit:66];
}
- (void)setDict:(NSDictionary *)dict{
    _dict = dict;
    self.lblName.text = [SDataUtil stringOf:dict[@"lgjunpn0lx1yl4wv"]];
}
- (void)downloadClickAction{
    
    NSArray *arrTemp = [SDataUtil arrayOf:_dict[@"vwggleltwbqhstqs"]];
    NSString *pathUrl = @"";
    if (arrTemp.count > 0){
        NSDictionary *dictTemp = [SDataUtil dictOf:arrTemp.firstObject];
        pathUrl = [SDataUtil stringOf:dictTemp[@"url"]];
    }
    if (pathUrl == nil || pathUrl.length < 1){
        [SHUD showInfo:KLanguage(@"down_fail_txt")];
        return;
    }
    [SHUD showScreenWait:@""];
    WEAK_SELF;
    [YMNetwork downloadWithUrl:pathUrl success:^(NSURL * _Nonnull path) {
        [SHUD hide];
        if (weakSelf.downClickBlock){
            weakSelf.downClickBlock(path);
        }
    } failure:^(NSError * _Nonnull error) {
        [SHUD hide];
        [SHUD showInfo:KLanguage(@"down_fail_txt")];
    }];
}

#pragma mark - setter and getter
- (UIView *)vBg{
    if (_vBg == nil){
        _vBg = [[UIView alloc] init];
        _vBg.backgroundColor = STheme.D9;
        _vBg.layer.cornerRadius = [SFitUtil fit:8];
        _vBg.clipsToBounds = YES;
    }
    return _vBg;
}
- (UILabel *)lblName{
    if (_lblName == nil){
        _lblName = [[UILabel alloc] init];
        _lblName.textColor = STheme.D1;
        _lblName.font = [SFitUtil fitBoldFont:14];
    }
    return _lblName;
}
- (UIImageView *)imgvIcon{
    if (_imgvIcon == nil){
        _imgvIcon = [[UIImageView alloc] init];
        _imgvIcon.image = [UIImage imageNamed:@"mine_product_specification_icon"];
    }
    return _imgvIcon;
}
- (UIButton *)btnDownload{
    if (_btnDownload == nil){
        _btnDownload = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnDownload setImage:[UIImage imageNamed:@"mine_product_specification_download"] forState:UIControlStateNormal];
        [_btnDownload addTarget:self action:@selector(downloadClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnDownload;
}


@end
